#include "certinfowindow.h"
#include "ui_certinfowindow.h"

#ifndef ENABLE_GMSSL
#include <openssl/x509.h>
#endif

#include <QLabel>

#include "constants.h"


CertInfoWindow::CertInfoWindow(QWidget *parent) :
  QFrame(parent),
  ui(new Ui::CertInfoWindow)
{
  ui->setupUi(this);




}

CertInfoWindow::~CertInfoWindow()
{
  delete ui;
}

void CertInfoWindow::ShowCert(X509 *cert)
{
#ifndef ENABLE_GMSSL
  if(cert_ != cert) {
    cert_ = cert;
    ui->tabWidget->setCurrentIndex(0);
  }

  {
    char subject[X509_NAME_LEN];
    X509_NAME_oneline(X509_get_subject_name(cert), subject, X509_NAME_LEN);
    AddInfoItem(tr("name"), QString::fromUtf8(subject), 0);
    AddInfoItem(tr("name"), QString::fromUtf8(subject), 1);
  }

  {
    char issuer[X509_NAME_LEN];
    X509_NAME_oneline(X509_get_issuer_name(cert), issuer, X509_NAME_LEN);
    QString issuer_string = QString::fromUtf8(issuer);
    AddInfoItem(tr("issuer"), issuer_string, 0);
    AddInfoItem(tr("issuer"), issuer_string, 1);
  }

  {
    long version = 0;
    X509_get_version(cert);
    AddInfoItem(tr("version"), QVariant::fromValue(version), 0);
    AddInfoItem(tr("version"), QVariant::fromValue(version), 1);
  }

  {
    ASN1_INTEGER* serial_num = X509_get_serialNumber(cert);
    int64_t int64_serial_num = 0L;
    ASN1_INTEGER_get_int64(&int64_serial_num, serial_num);
    QString str_serial_num;
    str_serial_num.sprintf("%llx", int64_serial_num);
    AddInfoItem(tr("serial number"), str_serial_num, 1);
  }

  ui->basicInfoFrame->layout()->addItem(
        new QSpacerItem(20, 20,
                        QSizePolicy::Expanding,
                        QSizePolicy::Expanding));
  ui->detailInfoFrame->layout()->addItem(
        new QSpacerItem(20, 20,
                        QSizePolicy::Expanding,
                        QSizePolicy::Expanding));
  show();
  activateWindow();
#endif
}

void CertInfoWindow::AddInfoItem(
    const QString &label,
    const QVariant &info,
    int page_index) {

  if(page_index == 0) {
    ui->basicInfoFrame->layout()->addWidget(
          GetInfoItem(label, info, ui->basicInfoFrame));
  } else if(page_index == 1) {
    ui->detailInfoFrame->layout()->addWidget(
          GetInfoItem(label, info, ui->detailInfoFrame));
  }

}

QWidget *CertInfoWindow::GetInfoItem(
    const QString &label,
    const QVariant &info,
    QWidget* parent) {

  auto* item = new QWidget(parent);
  QString text_info = info.toString();

  QLabel* name_label = new QLabel(label, item);
  name_label->setAlignment(Qt::AlignRight | Qt::AlignTop);
  QLabel* info_label = new QLabel(text_info, item);
  info_label->setWordWrap(true);
  info_label->setAlignment(Qt::AlignTop);
  QHBoxLayout* layout = new QHBoxLayout;
  layout->setSpacing(6);
  layout->setStretch(0, 1);
  layout->setStretch(1, 2);
  name_label->setMaximumWidth(CERT_INFO_WINDOW_NAME_WIDTH);
  name_label->setMinimumWidth(CERT_INFO_WINDOW_NAME_WIDTH);
  layout->addWidget(name_label);
  layout->addWidget(info_label);

  item->setLayout(layout);
  return item;

}

void CertInfoWindow::closeEvent(QCloseEvent *event)
{
  ui->basicInfoFrame->layout()->children();


}
